package cn.yshujia.repository;

import cn.yshujia.domain.dto.PageDTO;
import cn.yshujia.domain.entity.Website;
import cn.yshujia.domain.vo.LabelVO;
import cn.yshujia.domain.vo.PageVO;
import cn.yshujia.domain.vo.WebsiteVO;
import cn.yshujia.mapper.WebsiteMapper;
import cn.yshujia.transform.WebsiteTransform;
import cn.yshujia.utils.PageUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author YShuJia
 * @create 2025/1/10
 * @description WebsiteRepository
 */

@Repository
public class WebsiteRepository {
	
	@Resource
	private RedisTemplate<String, WebsiteVO> redis;
	
	@Resource
	private WebsiteMapper mapper;
	
	
	@Resource
	private LabelRepository labelRepository;
	
	
	public static final String VO = "website";
	
	public static final String RECOMMEND_LIST = "website:recommend:";
	
	public static final Duration WEBSITE_DURATION = Duration.ofDays(3L);
	
	public PageVO<WebsiteVO> pageByIsRecommend(PageDTO dto) {
		Map<String, Object> map = dto.getParam();
		
		if (ObjectUtils.isEmpty(map) || null == map.get("isRecommend")) {
			return new PageVO<>();
		}
		
		boolean isRecommend = Boolean.parseBoolean(map.get("isRecommend").toString());
		
		// 判断缓存中是否存在
		long start = (long) (dto.getCurrent() - 1) * dto.getSize();
		List<WebsiteVO> list = redis.opsForList().range(RECOMMEND_LIST + isRecommend, start, start + dto.getSize() - 1);
		
		if (!ObjectUtils.isEmpty(list)) {
			return PageUtils.getPage(dto, list, redis.opsForList().size(RECOMMEND_LIST + isRecommend));
		}
		
		// 获取数据库数据
		QueryWrapper<Website> wrapper = PageUtils.createWebsiteWrapper(dto);
		
		IPage<Website> page = mapper.selectPage(new Page<>(dto.getCurrent(), dto.getSize()), wrapper);
		
		if (!ObjectUtils.isEmpty(page) && !ObjectUtils.isEmpty(page.getRecords())) {
			return PageUtils.getPage(dto, createVOList(page.getRecords()), page.getTotal());
		}
		
		return new PageVO<>();
	}
	
	
	public List<WebsiteVO> createVOList(List<Website> list) {
		if (ObjectUtils.isEmpty(list)) {
			return new ArrayList<>();
		}
		List<WebsiteVO> voList = new ArrayList<>(list.size());
		for (Website website : list) {
			WebsiteVO vo = createVO(website);
			voList.add(vo);
		}
		return voList;
	}
	
	public WebsiteVO createVO(Website website) {
		WebsiteVO vo = WebsiteTransform.entity2VO(website);
		LabelVO labelVO = labelRepository.get(website.getLabelId());
		if (null != labelVO) {
			vo.setLabel(labelVO);
		}
		return vo;
	}
	
	@Async ("Task")
	public void del() {
		Set<String> keys = redis.keys(VO + "*");
		redis.delete(keys);
	}
}
